<?php
/**
 * @file $HeadURL: forum.php $
 * @author $Author: DongJC (dongjianchang@haibao.com) $
 * @date $Date: 2015-4-24 ����3:56:27 $
 * @brief 
 */
namespace haibao\cms\data;


class Forum extends \haibao\cms\data\BaseMysql{
	public function __construct(){
		parent::__construct('\haibao\cms\model\data\Celebrity');
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
	}
	
	public function addSign($data){
		$time = time();		
		$sql="insert into hb_bussiness_sign_info (uid,sign_time,this_credit) values (%s,%s,%s)";
		$this->query($sql,array($data['userId'],$time,$data['credit']));
	}
	
	/**
	 *
	 * @param array $data 用户的数据 id和credit：积分
	 * @param int $day 0为今天，1为昨天，2为前天，依次向之前的日期推算
	 * @return Ambigous <number, NULL, \DateTime>
	 */
	public function isSign($data,$day=0){
		$isHas = 0;
		$date = date('Y-m-d');
		$starTime = strtotime($date)-86400*$day;
		$endTime = strtotime($date.' 23:59:59')-86400*$day;
		$sql="select count(sid) as count from hb_bussiness_sign_info where uid = ".$data['userId']." and sign_time>=$starTime and sign_time<= $endTime ";
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			$isHas = $result->getData('count')?$result->getData('count'):0;
		}
		if($isHas){
			return false;
		}else{
			return true;
		}
	}
	
	/**
	 * 得到一周的签到情况
	 * @param unknown $userId
	 * @return Ambigous <multitype:, NULL, \DateTime>
	 */
	public function getWeekSign($userId){
		$data = array();
		$i = 0;
		$day = date("w");
		$today = date('Y-m-d');
		$statime = strtotime($today)-86400*($day-1);
		$endtime = strtotime($today)+86400*(7-$day);
		$sql="select this_credit,sign_time from hb_bussiness_sign_info where uid = %s and sign_time>='%s' and sign_time<='%s' ";
		$result = $this->query($sql,array($userId,$statime,$endtime));
		while($result->fetch_assoc()){
			$data[$i]['credit'] = $result->getData('this_credit');
			$data[$i]['signTime'] = $result->getData('sign_time');
			$i++;
		}
		return $data;
	}
	
	/**
	 * 获取版块发帖数
	 * @param int $fid
	 */
	public function getForumPostNum($fid){
		$cahceModel = new \haibao\cms\cache\Forum();
		$data = $cahceModel->getThreadsDataByFid($fid);
		if(empty($data)){
			$data = -1;
			$sql = 'select threads from hb_forum_forum where fid=%s';
			$result = $this->query($sql,array($fid));
			while($result->fetch_assoc()){
				$data = $result->getData('threads');
			}
			$cahceModel->setThreadsData($fid, $data);
		}
		return $data;
	}

	/**
	 * 获取论坛发帖会员
	 */
	public function getForumUsers($page=1,$name=''){
		$where = $name?" and username like '%".$name."%'":'';
		$limitFrom = ($page-1)*10;
		$sql = 'select uid,username from hb_common_member WHERE adminid>0'.$where.' limit '.$limitFrom.',10';
		$result = $this->query($sql);
		$res = array();
		while($result->fetch_assoc()){
			$data = array();
			$data['Id'] = $result->getData('uid');
			$data['UserName'] = $result->getData('username');
			array_push($res,$data);
		}
		return $res;
	}

	public function checkForumPostId($postId){
		$sql = "select a.subject from hb_forum_thread a left join hb_common_member b on a.authorid = b.uid WHERE a.tid = %s and b.adminid > 0";
		$result = $this->query($sql,array($postId));
		$resData = '';
		while($result->fetch_assoc()){
			$resData = $result->getData('subject');
		}
		$this->setConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS);
		$existSql = 'select Title from BBSPostActivity WHERE PostId = %s limit 1';
		$getExistData = $this->query($existSql,array($postId));
		while($getExistData->fetch_assoc()){
			$resData = $getExistData->getData('Title') ? '' :$resData;
		}
		return $resData;
	}

	public function addBbsPostActvity($model){
		$this->changeCMSConfig();
		$this->add($model);
	}

	public function getPostList($tid,$page){
		$guises = \haibao\cms\business\Guise::getAllGuise();
		$admins = $this->getAllAdminMember();
		$excludeUsers = \haibao\cms\business\Forum::getPrizeByPostId($tid);
		$excludeUsers = empty($excludeUsers[2][0]) ? array() : $excludeUsers[2];
		$notIn = '';
		$resData = array('count'=>0,'data'=>array());
		if($guises){
			$notIn = ' and authorid not in('.implode(',',array_merge($guises,$admins,$excludeUsers)).')';
		}
		$sql = "select pid,message,authorid,author,dateline from hb_forum_post where tid=%s and invisible=0 ".$notIn." order by dateline desc limit ".(($page-1)*10).",10";
		$sqlTotal = "select count(*) as countnum from hb_forum_post where tid=%s and invisible=0 ".$notIn;
		$posts = $this->query($sql,array($tid));
		$total = $this->query($sqlTotal,array($tid));
		$tag = 0;
		while($posts->fetch_assoc()){
			$resData['data'][$tag]['content'] = $posts->getData('message');
			$resData['data'][$tag]['id'] = $posts->getData('pid');
			$resData['data'][$tag]['uid'] = $posts->getData('authorid');
			$resData['data'][$tag]['datetime'] = date('Y-m-d H:i:s',$posts->getData('dateline'));
			$resData['data'][$tag]['name'] = $posts->getData('author');
			$tag++;
		}
		while($total->fetch_assoc()){
			$resData['count'] = $total->getData('countnum');
		}
		return $resData;
	}

	public function getPrizeByPostId($filter){
		$this->changeCMSConfig();
		return $this->getOne($filter);
	}

	public function addWinnerId($postId,$winnerIds){
		$this->changeCMSConfig();
		$sql = 'update BBSPostActivity set WinnerId = %s WHERE PostId = %s';
		$this->query($sql,array($winnerIds,$postId));
	}

	public function addExcludeId($postId,$excludeIds){
		$this->changeCMSConfig();
		$sql = 'update BBSPostActivity set ExcludeId = %s WHERE PostId = %s';
		$this->query($sql,array($excludeIds,$postId));
	}

	public function addPrizeMessage($postId,$content){
		$this->changeCMSConfig();
		$sql = 'update BBSPostActivity set Message = %s WHERE PostId = %s';
		$this->query($sql,array($content,$postId));
	}

	public function getNickNameByIds($uids){
		$sql = "SELECT uid,username FROM hb_common_member where uid in (".implode(',',$uids).")";
		$result = $this->query($sql);
		$resData = array();
		while($result->fetch_assoc()){
			$resData[$result->getData('uid')]['NickName'] = $result->getData('username');
			$resData[$result->getData('uid')]['RealName'] = '';
			$resData[$result->getData('uid')]['Address'] = '';
			$resData[$result->getData('uid')]['Phone'] = '';
			$resData[$result->getData('uid')]['Email'] = '';
		}
		return $resData;
	}

	private function changeCMSConfig(){
		$this->setConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS);
	}

	private function getAllAdminMember(){
		$sql = "SELECT uid FROM hb_common_member where adminid>0";
		$result = $this->query($sql);
		$adminIds = array();
		while($result->fetch_assoc()){
			$adminIds[] = $result->getData('uid');
		}
		return $adminIds;
	}

}